#!/usr/bin/python
deafult_ta_pub_key_modulus=[
        0xa6, 0x5a, 0x18, 0xad, 0xc0, 0xb3, 0xce, 0xe3,
        0x0c, 0x90, 0x2b, 0x7d, 0x40, 0x9b, 0xac, 0xbb,
        0x5a, 0x49, 0x88, 0x42, 0x0a, 0xb1, 0xe0, 0x92,
        0xc0, 0x86, 0x91, 0xe6, 0x9e, 0xef, 0xa0, 0xe1,
        0xf1, 0x2f, 0x90, 0xf6, 0x66, 0xdd, 0xea, 0x15,
        0x5a, 0xc0, 0x28, 0xb4, 0x9d, 0xdf, 0x52, 0x0c,
        0x6e, 0xa5, 0xc6, 0x19, 0x43, 0xac, 0x69, 0xa8,
        0xf2, 0x5f, 0xfe, 0x60, 0x00, 0xa5, 0xd2, 0x3a,
        0xcc, 0x12, 0x01, 0xf3, 0x5b, 0xba, 0x0c, 0x51,
        0x1c, 0x2c, 0xf4, 0xb0, 0x77, 0xb9, 0xd6, 0x72,
        0x35, 0x1c, 0x0f, 0x73, 0xcd, 0x2f, 0x16, 0xf9,
        0xba, 0x89, 0x84, 0xa0, 0x9b, 0xbf, 0x8b, 0x5b,
        0x7f, 0xe8, 0x8b, 0x8b, 0x9f, 0xc8, 0x71, 0xea,
        0x1c, 0x6c, 0x1f, 0x00, 0xa0, 0xee, 0xca, 0x3e,
        0xbf, 0x18, 0x8e, 0x57, 0x9b, 0x48, 0x66, 0xeb,
        0x8b, 0xd4, 0x40, 0x51, 0xae, 0x86, 0xa1, 0x86,
        0xf8, 0xd0, 0x0b, 0xfa, 0xb6, 0x2e, 0x25, 0x2e,
        0x05, 0xff, 0x76, 0x01, 0x46, 0xd7, 0x3d, 0xeb,
        0x1f, 0x7c, 0xfc, 0xdf, 0x1e, 0x82, 0x8b, 0x5e,
        0xb0, 0xca, 0xa3, 0xea, 0x16, 0xbe, 0x2d, 0x6e,
        0x54, 0x50, 0x9c, 0xb8, 0x41, 0xef, 0x17, 0xc7,
        0x7f, 0x60, 0x54, 0xaa, 0xf9, 0x70, 0xa0, 0xf8,
        0x98, 0x2e, 0x82, 0xf9, 0x8c, 0x5f, 0xfb, 0x9c,
        0x88, 0x8e, 0x77, 0xb0, 0x32, 0x72, 0x99, 0x1a,
        0xe0, 0x04, 0x5e, 0xbf, 0x1d, 0xaa, 0xf7, 0xf0,
        0x05, 0x3c, 0xb4, 0x95, 0xdd, 0x4d, 0xba, 0x3a,
        0x71, 0xa1, 0xf8, 0x72, 0x8e, 0x1d, 0x98, 0xe3,
        0x13, 0xba, 0xca, 0xb0, 0x27, 0xbf, 0x87, 0x59,
        0x54, 0x27, 0xf8, 0xe8, 0xec, 0xc6, 0x85, 0x37,
        0xd2, 0x3a, 0x95, 0xe4, 0xa5, 0x09, 0x9a, 0xd0,
        0x1f, 0xa3, 0x86, 0xb0, 0x92, 0xd8, 0x02, 0x1e,
        0x33, 0x75, 0xdb, 0x26, 0xbf, 0x27, 0xfc, 0x35,
]


def get_args():
	import argparse

	parser = argparse.ArgumentParser()
	parser.add_argument('--in_file', required=True, \
		help='Name of in file')

	parser.add_argument('--out_file', required=True, \
		help='Name of out file')

	parser.add_argument('--key', required=True, help='Name of key file')

	return parser.parse_args()

def main():
        from Crypto.PublicKey import RSA
        from Crypto.Util.number import long_to_bytes
        args = get_args()
        f = open(args.in_file,'rb')
        raw = f.read()
        f.close()
        f = open(args.key, 'r')
        key = RSA.importKey(f.read())
        f.close

        byteDef=bytearray(deafult_ta_pub_key_modulus)
        newPubKey=bytearray(long_to_bytes(key.publickey().n))
        firstMatch = raw.find(byteDef)
        lastMatch = raw.rfind(byteDef)
        if firstMatch != -1:
            if firstMatch != lastMatch:
                print("more than one match found")
                exit(2)
            else:
                raw = raw.replace(byteDef,newPubKey)
                f = open(args.out_file,'wb')
                f.write(raw)
                f.close()
                exit(0)
        else:
                print("default pubkey not found")
                exit(1)

if __name__ == "__main__":
        main()
